package NWish.properties.object3ds
{
	
	import NWish.Logger;
	import NWish.json.OrderDict;
	import NWish.properties.Properties;
	import NWish.properties.materials.AnimatedBitmapMaterialProperties;
	import NWish.properties.materials.BitmapMaterialProperties;
	import NWish.properties.materials.ColorMaterialProperties;
	import NWish.properties.materials.SkyBoxMaterialProperties;
	
	import away3d.containers.Scene3D;
	import away3d.materials.*;
	
	import NWish.Interface.ILogger;
	
	
	public class MaterialProperties
	{
		private var _parent;
		
		
		static var _MaterialTypeStrs:Array = 
			['ColorMaterial'
			,'BitmapMaterial'
			//,'BitmapFileMaterial'
			,'AnimatedBitmapMaterial'
			//,'SegmentMaterial'
			,'SkyBoxMaterial'
			//,'VideoMaterial'
			];
		
		
		public function MaterialProperties(parent)
		{
			_parent = parent;
		}
		
		public function propertiesToDict(obj,dict:OrderDict,read_only,saving = false)
		{
			if(read_only == false)
			{
				if(saving)
				{
					saveMaterial(dict,obj.material);
				}
				else
				{
					if(obj.material)
					{
						var mat = 
							{
								'type':mt2str(obj.material),
								'name':obj.material.name
							};
						dict.push('material', mat);
					}
					else
					{
						dict.push('material', null);
					}
					
					dict.push(Properties.readMark + 'material type',_MaterialTypeStrs);
				}
			}
		}
		
		public function dictToProperties(dict,obj,scene:Scene3D,isloading)
		{
			var mat = dict['material'];
			
			if(mat == null)
			{//load from file
				loadMaterial(dict,obj,scene);
			}
			else if(mat is String)
			{
				newMat(mat,obj,scene);
			}
			else if(mat)
			{
				for(var k in mat)
				{
					if(k == 'type')
					{
						var type = mat[k];
						newMat(type,obj,scene);
					}
					else if(k == 'name')
					{
						var name = mat[k];
						if(name && obj.material && obj.material.name != name)
						{
							obj.material.name = name;
						}
					}
					else
					{
						ILogger.log('Undefined Propertie [' + k + ']',obj);
					}
				}
			}
		}
		
		protected function mt2str(mat)
		{
			var mt:String = mat + '';
			mt = mt.replace('[','');
			mt = mt.replace(']','');
			mt = mt.replace('object ','');
			return mt;
		}
		
		protected function newMat(type:String,obj,scene)
		{
			if(type && type != mt2str(obj.material))
			{
				var mat = newMat1(type);
				if(mat)
				{
					mat.ambientColor = scene.extra.ambientColor;
					
					obj.material = mat;
					
					if(_parent)
						obj.material.lights = _parent.getPlugin('Away3d').lights;
				}
				else
					ILogger.log('Can not find the material type[' + type + ']',obj);
			}
		}
		
		private function newMat1(mat:String)
		{
			switch(mat)
			{
				case 'ColorMaterial':
					return new ColorMaterial(0xffffff);
				case 'BitmapMaterial':
					return new BitmapMaterial();
					//case 'BitmapFileMaterial':
					//	return new BitmapFileMaterial();
				case 'AnimatedBitmapMaterial':
					return new AnimatedBitmapMaterial();
					//ILogger.log('to do : implement AnimatedBitmapMaterial initialize');
				case 'SegmentMaterial':
					return new SegmentMaterial();
				case 'SkyBoxMaterial':
					return new SkyBoxMaterial(null);
				case 'VideoMaterial':
					ILogger.log('to do : implement VideoMaterial initialize');
					return null;
				default :
					return null;
			}
		}
		
		//
		protected function saveMaterial(dict,mat)
		{
			if(mat is ColorMaterial)
			{
				new ColorMaterialProperties(_parent,null).save(mat,dict);
			}
			else if (mat is AnimatedBitmapMaterial)
			{
				new AnimatedBitmapMaterialProperties(_parent,null).save(mat,dict);
			}
			else if (mat is BitmapMaterial)
			{
				new BitmapMaterialProperties(_parent,null).save(mat,dict);
			}
			else if (mat is SkyBoxMaterial)
			{
				new SkyBoxMaterialProperties(_parent,null).save(mat,dict);
			}
			else if (mat is SegmentMaterial)
			{
				
			}
			else if (mat is VideoMaterial)
			{
				
			}
			else if (mat == null)
			{
				
			}
			else
			{
				
			}
		}
		
		protected function loadMaterial(dict,obj,scene)
		{
			if (dict['ColorMaterial'])
			{
				obj.material
					= new ColorMaterialProperties(_parent,null).load(dict,obj.material,scene);
			}
			else if (dict['BitmapMaterial'])
			{
				obj.material
					= new BitmapMaterialProperties(_parent,null).load(dict,obj.material,scene);
			}
			else if (dict['SkyBoxMaterial'])
			{
				new SkyBoxMaterialProperties(_parent,null).load(dict,obj.material,scene);
			}
			else if (dict['AnimatedBitmapMaterial'])
			{
				obj.material
					= new AnimatedBitmapMaterialProperties(_parent,null).load(dict,obj.material,scene);
			}
			else if (dict['SegmentMaterial'])
			{
				
			}
			else if (dict['VideoMaterial'])
			{
				
			}
			else
			{
				if(obj.material)
				{
					//throw new Error();
				}
				else
					obj.material = new ColorMaterial(0x00ffff,1);
				
				if(_parent)
					obj.material.lights = _parent.getPlugin('Away3d').lights;
			}
			
			
			if(obj.material)
			{
				obj.material.ambientColor = scene.extra.ambientColor;
			}
			
		}
		
		
	}
	
	

}